package com.FH.applyForm.officeServies.export.vacation;

import com.FH.util.AccessDB;
import com.FH.util.ExportExcel;
import com.FH.util.ExportPDF;
import com.FH.util.ResultDTO;
import com.itextpdf.text.DocumentException;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import javax.ejb.Stateless;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Stateless
public class VacationDAOImpl extends AccessDB implements VacationDAO {


    //根据条件导出休假清单
    @Override
    public ResultDTO exportVacationExcel(String condition) {

        ResultDTO resultDTO = new ResultDTO();
        String downloadUrl = "";

        String pdfDownLoadUrl = "";

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");
        List<String[]> seresult = new ArrayList<String[]>();
        List<VacationShowDTO> result = new ArrayList<VacationShowDTO>();

        try {


            String sql = "select km.member_name,km.member_gender,km.member_phone,km.member_email,o.organization_name, " +
                    "vf.vacation_form_disdays,vf.vacationresion,vt.vacation_type,vf.vacation_form_starttime,vf.vacation_form_endtime " +
                    "from vacation_form vf " +
                    "left join kam_member km " +
                    "on km.member_id = vf.member_id " +
                    "left join organization o " +
                    "on km.organization_id = o.organization_id " +
                    " left join vacationtype vt " +
                    "on vf.vacationtypename = vt.vacationtypename " +
                    "where 1=1 ";


            JSONObject search = new JSONObject(condition);

            if (search.has("starttime")) {
                if(null != search.get("starttime").toString() && !search.get("starttime").toString().isEmpty()){
                    sql += " and vf.vacation_createtime >'" + search.get("starttime").toString() + "' ";
                }
            }
            if (search.has("endtime")) {
                if(null != search.get("endtime").toString() && !search.get("endtime").toString().isEmpty()){
                    sql += " and vf.vacation_createtime < '" + search.get("endtime").toString() + "' ";
                }
            }
            if (search.has("member_id")) {
                if(null != search.get("member_id").toString() && !search.get("member_id").toString().isEmpty()){
                    sql += "and km.member_id = '" + search.get("member_id").toString() + "' ";
                }
            }
            if (search.has("organization_id")) {
                if(null != search.get("organization_id").toString() && !search.get("organization_id").toString().isEmpty()){
                    sql += "and o.organization_id ='" + search.get("organization_id").toString() + "' ";
                }
            }
            if (search.has("isverify")) {
                if(null != search.get("isverify").toString() && !search.get("isverify").toString().isEmpty()){
                    sql += "and vf.isverify ='" + search.get("isverify").toString() + "' ";
                }
            }




            seresult = this.getReportingTo(sql);
            for (int i = 0; i < seresult.size(); i++) {
                VacationShowDTO vacationShowDTO = new VacationShowDTO();
                vacationShowDTO.setMember_name(seresult.get(i)[1]);
                if (seresult.get(i)[2].equals("0")) {
                    vacationShowDTO.setMember_gender("男");
                    seresult.get(i)[2] = "男";
                }
                if (seresult.get(i)[2].equals("1")) {
                    vacationShowDTO.setMember_gender("女");
                    seresult.get(i)[2] = "女";
                }
                vacationShowDTO.setMember_phone(seresult.get(i)[3]);
                vacationShowDTO.setMember_email(seresult.get(i)[4]);
                vacationShowDTO.setOrganization_name(seresult.get(i)[5]);
                vacationShowDTO.setVacation_form_disdays(seresult.get(i)[6]);
                vacationShowDTO.setVacationresion(seresult.get(i)[7]);
                vacationShowDTO.setVacation_typevf(seresult.get(i)[8]);
                vacationShowDTO.setVacation_form_starttime(seresult.get(i)[9]);
                vacationShowDTO.setVacation_form_endtime(seresult.get(i)[10]);
                result.add(vacationShowDTO);
            }


            String[] headers = {"姓名", "性别", "电话", "邮箱", "部门", "请假天数", "请假理由", "请假类型", "开始时间", "结束时间"};
            String title = "休假统计";
            String filename = "休假统计表";
            ExportExcel<VacationShowDTO> exportExcel = new ExportExcel<VacationShowDTO>();
            downloadUrl = exportExcel.createExcelFile(exportExcel, title, headers, result, filename);

            try {
                pdfDownLoadUrl = ExportPDF.createPDFFile(seresult, title, headers, filename);
            } catch (DocumentException e) {
                e.printStackTrace();
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

        if(null != downloadUrl && "" != downloadUrl){
            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(downloadUrl);
            resultDTO.setStr(pdfDownLoadUrl);
            resultDTO.setValues(result);
        }
        else{
            resultDTO.setStatus("90001");
        }

        return resultDTO;
    }
}
